<!-- Dom页面 -->
<template>
  <div id="Login">
    <!-- 登录窗体 -->
    <div class="loginDiv">
      <!-- el的form表单标签 -->
      <el-form :model="loginForm" ref="login">
        <h3 class="title">登录</h3>
        <el-form-item prop="nickName">
          <el-input
            type="text"
            prefix-icon="el-icon-user"
            v-model="loginForm.nickName"
            placeholder="用户名（昵称）"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item prop="pwd">
          <el-input
            type="password"
            :show-password="true"
            prefix-icon="el-icon-lock"
            v-model="loginForm.pwd"
            placeholder="请输入密码"
            autocomplete="off"
          ></el-input>
          <el-link type="primary" @click="registerShow=true">没有账号？点击注册</el-link>
        </el-form-item>
        <el-form-item>
          <el-button
            style="width:100%;letter-spacing: 15px;"
            type="primary"
            plain
            @click="toLogin"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>

    <!-- 用户注册弹窗 -->
    <el-dialog
      title="用户注册"
      :visible.sync="registerShow"
      width="40%"
      :before-close="registerHandleClose"
      :close-on-click-modal="false"
    >
      <el-form :model="registerData" ref="register">
        <el-form-item prop="realName">
          <el-input
            type="text"
            prefix-icon="el-icon-user"
            v-model="registerData.realName"
            placeholder="真实姓名"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item prop="nickName">
          <el-input
            type="text"
            prefix-icon="el-icon-user"
            v-model="registerData.nickName"
            placeholder="用户名（昵称、尽量使用英文）"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item prop="sex">
          <el-radio v-model="registerData.sex" :label="1">男</el-radio>
          <el-radio v-model="registerData.sex" :label="2">女</el-radio>
        </el-form-item>
        <el-form-item prop="tellNumber">
          <el-input
            type="text"
            prefix-icon="el-icon-user"
            v-model="registerData.tellNumber"
            placeholder="手机号码"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item prop="pwd">
          <el-input
            type="password"
            :show-password="true"
            prefix-icon="el-icon-lock"
            v-model="registerData.pwd"
            placeholder="请定义输入密码"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item prop="pwd2">
          <el-input
            type="password"
            :show-password="true"
            prefix-icon="el-icon-lock"
            v-model="registerData.pwd2"
            placeholder="请确认密码"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            style="width:100%;letter-spacing: 15px;"
            type="primary"
            plain
            @click="saveUser"
            >保存</el-button
          >
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<!-- 组件js -->
<script>
import * as API from '@/api/user.js'

export default {
  name: "Login",
  data() {
    return {
      // vue 数据双向绑定的对象
      loginForm: {
        nickName: null,
        pwd: null
      },
      // 注册框体显示隐藏属性
      registerShow: false,
      // 用户注册信息
      registerData:{
        realName:null,
        nickName:null,
        sex:1,
        tellNumber:null,
        pwd:null,
        pwd2:null
      }
    };
  },
  methods:{
    // 当注册表单关闭后触发
    registerHandleClose(){
      this.registerData = {
        realName:null,
        nickName:null,
        sex:1,
        tellNumber:null,
        pwd:null,
        pwd2:null
      };
      this.registerShow = false; // 关闭窗体
    },
    // 点击登录按钮操作
    toLogin(){
      // 1.验证用户名密码
      let _this = this;
      API.doLogin(this.loginForm)
      .then(function(res){
          if(res.code == 200){
              console.dir(res);

              window.localStorage.setItem("token",res.data.token); // 保存用户token
              window.localStorage.setItem("user",JSON.stringify(res.data.user)); // 保存用户信息

              // 2. 转到聊天主界面
              _this.$router.push("/main");
          }
      })
      
    },
    saveUser(){
      var _this = this;
      API.add(this.registerData)
      .then(function(res){
          if(res.code == 200){
              _this.registerShow = false;
          }
      })
    }
  }
};
</script>

<!-- CSS代码 -->
<style scoped>
#Login {
  background: linear-gradient(193deg, #e0c9b8, #5a6f7c);
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

.loginDiv {
  width: 380px;
  height: 390px;
  background-color: white;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  border-radius: 4px;
  box-sizing: border-box;
  padding: 70px 20px 0px 20px;
}

.title {
  text-align: center;
  letter-spacing: 15px;
}
</style>
